package com.atguigu.bigdata.spark.core.rdd.operator.transform;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.FlatMapFunction;
import org.apache.spark.sql.sources.In;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public class Spark04_RDD_Operator_Transform_JAVA {
    public static void main(String[] args) {
        //flatmap 算子
        SparkConf conf = new SparkConf().setMaster("local[*]").setAppName("sparkCore");
        JavaSparkContext sc = new JavaSparkContext(conf);

        List<Integer> list1 = Arrays.asList(1,2,3);
        List<Integer> list2 = Arrays.asList(4,5,6);
        List<List<Integer>> list = new ArrayList<>();
        list.add(list1);
        list.add(list2);
        JavaRDD<List<Integer>> rdd = sc.parallelize(list, 2 );
        JavaRDD<Integer> flatmap = rdd.flatMap(new FlatMapFunction<List<Integer>, Integer>() {
            @Override
            public Iterator<Integer> call(List<Integer> integers) throws Exception {
                return integers.iterator();
            }
        });

        List<Integer> res = flatmap.collect();
        for(Integer val : res) {
            System.out.println(val);
        }
        sc.stop();
    }
}
